﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Configuration;
using System.Xml;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Threading;

namespace VanTaiTMS.Page
{
    public partial class Config_web : System.Web.UI.Page
    {
        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowConfig();
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtServerName.Text == "" && txtDBName.Text == "" && txtUserName.Text == "" && txtPasswords.Text == "")
            {
                var configuration = WebConfigurationManager.OpenWebConfiguration("~");
                var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
                section.ConnectionStrings["Connect"].ConnectionString = lbStringConnect.Text;
                configuration.Save();
                ClearTextBox();
                upDatePanelAll.Update();
                OpenDialog("#thongbao");
                lbThongBaoLoi.Text = "Lưu thành công chuổi kết nối !";
                updatePanelThongBao.Update();
            }
            else
            {
                var configuration = WebConfigurationManager.OpenWebConfiguration("~");
                var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
                section.ConnectionStrings["Connect"].ConnectionString = "Server=" + txtServerName.Text + ";Database=" + txtDBName.Text + ";User ID=" + txtUserName.Text + ";Password=" + txtPasswords.Text + ";MultipleActiveResultSets=True;pooling=true;Max Pool Size=1000000;Connect Timeout=300000;";
                configuration.Save();
                ClearTextBox();
                upDatePanelAll.Update();
                OpenDialog("#thongbao");
                lbThongBaoLoi.Text = "Lưu thành công chuổi kết nối !";
                updatePanelThongBao.Update();
            }
        }
        protected void btnRefesh_Click(object sender, EventArgs e)
        {
            ShowConfig();
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }
        //--------------button-popup---------------------
        protected void cmdCloseThongBao_Click(object sender, EventArgs e)
        {
            CloseDialog("#thongbao");
        }
        #endregion

        #region[Function]
        protected void ShowConfig()
        {
            string sql = ConfigurationManager.ConnectionStrings["Connect"].ConnectionString;
            lbStringConnect.Text = sql.ToString();
        }
        #endregion

        #region[Clear-TextBox]
        protected void ClearTextBox()
        {
            txtServerName.Text = string.Empty;
            txtDBName.Text = string.Empty;
            txtUserName.Text = string.Empty;
            txtPasswords.Text = string.Empty;
        }
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        #endregion
    }
}